package com.goods.cart.controller;

import com.goods.cart.service.CartService;
import com.goods.entity.Cartitem;
import com.goods.entity.User;
import com.goods.povos.CartPovo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    @RequestMapping("/GetCartiermByUid")
    public String getCartiermByUid(String uid, Model model){
        List<CartPovo> cartPovos=cartService.getCartiermByUid(uid);
        model.addAttribute("cartPovos",cartPovos);
        return "jsps/cart/list";
    }

    @RequestMapping("/AddCartitem")
    public String addCartitem(Cartitem cartitem){
        cartService.addCartitem(cartitem);
        return "redirect:/cart/GetCartiermByUid?uid="+cartitem.getUid();
    }

    @RequestMapping("/Piliang")
    public String piliang(String[] ids, HttpSession session){
        User user = (User) session.getAttribute("user");
        String uid = user.getUid();
        System.out.println(ids);
        cartService.removeCartitemIn(ids);
        return "redirect:/cart/GetCartiermByUid?uid="+uid;
    }

    @RequestMapping("/RemoveCartByID")
    public String removeCartByID(String uid,String cartitemid){
        cartService.removeCartByID(cartitemid);
        return "redirect:/cart/GetCartiermByUid?uid="+uid;
    }

    @RequestMapping("/ChangCartQuantity")
    @ResponseBody
    public Boolean changCartQuantity(Cartitem cartitem){
        return cartService.changCartQuantity(cartitem);
    }

//    获取购物车准备生产订单
    @RequestMapping("/GetCartPovoIn")
    public  String  getCartPovoIn(String[] ids ,Model  model) {
        List<CartPovo>  povos =  cartService.getCartPovoIn(Arrays.asList(ids));
        model.addAttribute("povos", povos);
        return   "jsps/cart/showitem";
    }

}
